package com.shike.hq.controller;

import com.shike.hq.entity.Admin;
import com.shike.hq.entity.Area;
import com.shike.hq.entity.Unit;
import com.shike.hq.service.AreaService;
import com.shike.hq.service.UnitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;

/**
 * The type Unit controller.
 */
@Controller
@RequestMapping("/base")
public class UnitController {

  private static final String REDIRECT = "redirect:/base/unit";
  @Autowired
  UnitService unitService;

  @Autowired
  AreaService areaService;

  /**
   * 单位页面路径映射
   *
   * @param model   model类型
   * @param session session类型
   * @return 页面路径映射字段
   */
  @GetMapping("/unit")
  public String unit(Model model, HttpSession session) {
    Admin admin = (Admin) session.getAttribute("admin");
    long areaId = admin.getAreaId();
    ArrayList<Unit> units = unitService.selectAllUnitByAreaId(areaId);
    if (units.size() > 0) {
      //找到单位信息
      model.addAttribute("units", units);
    } else {
      model.addAttribute("units", "null");
    }
    ArrayList<Area> areas = areaService.selectAllAreaById(areaId);
    model.addAttribute("areas", areas);
    return "base/unit";
  }

  /**
   * 编辑单位信息
   *
   * @param unit  编辑后的单位信息
   * @param attributes model类型
   * @return 页面路径字符串
   */
  @PostMapping("/unit/update")
  public String updateUnit(Unit unit, RedirectAttributes attributes) {
    try {
      unitService.updateById(unit);
      attributes.addFlashAttribute("msg", "编辑成功！");
    } catch (Exception e){
      attributes.addFlashAttribute("msg", "编辑失败！");
    }
    return REDIRECT;
  }

  /**
   * 删除单位信息
   *
   * @param id         要删除的单位id
   * @param attributes model类型
   * @return 页面路径字符串
   */
  @GetMapping("/unit/delete")
  public String deleteUnit(@RequestParam("id") long id, RedirectAttributes attributes) {
    try {
      unitService.removeById(id);
      attributes.addFlashAttribute("msg", "删除成功！");
    } catch (Exception e) {
      attributes.addFlashAttribute("msg", "删除失败！该单位下仍有标签使用中！");
    }
    return REDIRECT;
  }

  /**
   * 删除多条单位信息
   *
   * @param idList     单位id列表
   * @param attributes model类型
   * @return 页面路径字符串
   */
  @GetMapping("/unit/deleteUnits")
  public String deleteUnits(@RequestParam("idList") ArrayList<Long> idList,
                            RedirectAttributes attributes) {
    try {
      unitService.removeByIds(idList);
      attributes.addFlashAttribute("msg", "删除成功！");
    } catch (Exception e) {
      attributes.addFlashAttribute("msg", "删除失败！");
    }
    return REDIRECT;
  }

  /**
   * 添加单位信息
   *
   * @param unit    添加的单位信息
   * @param attributes   model类型
   * @param session session对象
   * @return
   */
  @PostMapping("/unit/add")
  public String addUnit(Unit unit, RedirectAttributes attributes, HttpSession session) {
    unit.setCreateTime(new Timestamp(System.currentTimeMillis()));
    //unit.setInspectionId(1);
    //unit.setLatitude(0);
    //unit.setLongitude(0);
    try {
      unitService.save(unit);
      attributes.addFlashAttribute("msg", "添加成功！");
    } catch (Exception e){
      attributes.addFlashAttribute("msg", "添加失败！");
    }
    return REDIRECT;
  }
}
